package cn.mcsys.viewer.core.listener;

import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.viewer.entity.AirRideQrCode;
import cn.mcsys.viewer.core.utils.JedisUtilsViewer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoadUserETickets implements ServletContextListener {

    public static Map<Integer,List<AirRideQrCode>> qrCodeMap = new HashMap<>();

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("进入缓存");
//        使用redis的string类型,存储用户,存的时候用json,取的时候也用json
//        BackService backService = SocketServiceLoader.webApplicationContext.getBean(BackService.class);
//        List<AirRideQrCode> airRideQrCodes = backService.selectAllQRCode();
//        System.out.println(airRideQrCodes.size());
//        for (AirRideQrCode airRideQrCode : airRideQrCodes) {
//            if(airRideQrCode.getUser() != null){
//                String id = airRideQrCode.getUser().getId().toString();
//                String s = JedisUtilsViewer.get(airRideQrCode.getUser().getId().toString());
//                if(s == null){
//                    List<AirRideQrCode> qrCodes = new ArrayList<>();
//                    qrCodes.add(airRideQrCode);
//                    String jsonQrCodes = JSON.toJSONString(qrCodes);
//                    JedisUtilsViewer.set(id,jsonQrCodes,3600);
//                }else {
//                    List<AirRideQrCode> qrCodes = JSONArray.parseArray(s, AirRideQrCode.class);
//                    qrCodes.add(airRideQrCode);
//                    String jsonQrCodes = JSON.toJSONString(qrCodes);
//                    JedisUtilsViewer.set(id,jsonQrCodes,3600);
//                }
//            }
//        }
//        Map<Integer,List<AirRideQrCode>> qrCodeMap = new HashMap<>();
//        BackService backService = SocketServiceLoader.webApplicationContext.getBean(BackService.class);
//        List<AirRideQrCode> airRideQrCodes = backService.selectAllQRCode();
//        System.out.println(airRideQrCodes.size());
//        for (AirRideQrCode airRideQrCode : airRideQrCodes) {
//            System.out.println(airRideQrCode);
//            if(airRideQrCode.getUser() != null){
//                List<AirRideQrCode> airRideQrCodes1 = qrCodeMap.get(airRideQrCode.getUser().getId());
//                if(airRideQrCodes1 == null || airRideQrCodes1.size() == 0){
//                    List<AirRideQrCode> codes = new ArrayList<>();
//                    codes.add(airRideQrCode);
//                    qrCodeMap.put(airRideQrCode.getUser().getId().intValue(),codes);
//                }else {
//                    airRideQrCodes1.add(airRideQrCode);
//                }
//            }
//        }
//        System.out.println(qrCodeMap.size());
//        Set<Map.Entry<Integer, List<AirRideQrCode>>> entries = qrCodeMap.entrySet();
//        for (Map.Entry<Integer, List<AirRideQrCode>> entry : entries) {
//            System.out.println(entry.getKey()+":"+entry.getValue().toString());
//        }
    }
}
